<template>
  <div class="clearfix">
    <el-col :span="24" class="toolbar" style="text-align: right">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="pageNo"
        :page-sizes="pageSizes"
        :page-size="pageSizeNumber"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        ref="pagination"
      ></el-pagination>
    </el-col>
  </div>
</template>
<script>
export default {
  data() {
    return {
      //分页
      curPageNo: this.$store.getters.pageNo,
      total: 0,
      pageSizeNumber: 20,
      // pageSizes:[5,10,15,20]
      // defaultPageSizes:[10,20,30,40]
    }
  },
  //搜索条件
  props: [
    'filterField',
    'requestUrl',
    'pageSize',
    'pageSizes',
    'isPackParams',
    'pageNos',
  ],
  mounted() {},
  computed: {
    pageNo() {
      return this.$store.getters.pageNo
    },
  },
  methods: {
    getTableListData(currentPage, pageSize) {
      // var filterFieldName = this.filterField.area;
      // var filterFieldBrand = this.filterField.department;
      // //去掉两端空格
      // var filterFieldCategory = this.filterField.company.trim();
      if (!currentPage) {
        if (this.pageSize) {
          this.pageSizeNumber = this.pageSize
        } else {
          this.pageSizeNumber = 20
        }
        this.$refs.pagination.internalCurrentPage = 1
      }
      // if(currentPage)
      var pageS
      if (pageSize) {
        pageS = pageSize
        this.pageSizeNumber = pageSize
      } else if (this.pageSize) {
        pageS = this.pageSize
      } else {
        pageS = 20
      }

      var data = {
        pageNo: currentPage ? currentPage : 1,
        pageSize: pageS,
        // "filterFieldName":filterFieldName
      }

      // data.pageParam =  JSON.stringify(pageData);
      if (this.filterField) {
        // data.params = JSON.stringify(this.filterField);
        if (this.isPackParams) {
          data.params = JSON.stringify(this.filterField)
        } else {
          data = Object.assign(data, this.filterField)
        }
      }

      this.axios({
        method: 'post',
        url: this.requestUrl,
        data: data,
      })
        .then(
          (response) => {
            if (!response.data.hasError) {
              if (
                response.data.result.pageCount * 1 > 0 &&
                data.pageNo * 1 > response.data.result.pageCount * 1
              ) {
                data.pageNo = response.data.result.pageCount
                this.getTableListData(data.pageNo, data.pageSize)
              } else {
                // this.tableList = response.data.result.results;
                if (response.data.result.results) {
                  this.tableList = response.data.result
                } else {
                  this.tableList = []
                }
                //  this.tableList = response.data.result.results;
                // this.tableList = response.data.results;
                //传值到父组件
                this.$emit('getList', this.tableList)
                this.total = response.data.result.total
              }
            } else {
              this.$message({
                message: response.data.errorMsg,
                type: 'warning',
              })
              this.tableList = []
              this.$emit('getList', this.tableList)
            }
          },
          (response) => {
            this.tableList = []
            // //传值到父组件
            this.$emit('getList', this.tableList)
            this.$message({
              message: '数据获取失败',
              type: 'warning',
            })
            // this.total = response.data.total;
          }
        )
        .catch((error) => {
          this.tableList = []
          // //传值到父组件
          this.$emit('getList', this.tableList)
          // this.$message({
          //   message: "数据请求失败，请重试",
          //   type: "warning"
          // });
        })
    },
    //每页显示多少条
    handleSizeChange(val) {
      this.$emit('getpageSize', val)
      // 选中条数变为第一页
      this.pageNo = 1
      this.$store.commit('SET_INFO', ['pageNo', 1])
      // this.$emit("getPageSize",val);
      this.pageSizeNumber = val
      this.$refs.pagination.internalCurrentPage = this.pageNo
      this.getTableListData(this.pageNo, val)
    },
    //显示第几页
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.curPageNo = val
      this.$emit('getpageNo', val)
      this.getTableListData(val, this.pageSizeNumber)
    },
  },
}
</script>
<style scoped lang="less">
.clearfix:after {
  content: '.'; /*加一段内容*/
  display: block; /*让生成的元素以块级元素显示，占满剩余空间*/
  height: 0; /*避免生成的内容破坏原有布局高度*/
  clear: both; /*清除浮动*/
  visibility: hidden; /*让生成的内容不可见*/
}
.clearfix {
  zoom: 1; /*为IE6，7的兼容性设置*/
}
</style>
